<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
class CommonController extends Controller
{
    //显示登陆页面
    public function login()
    {
       return view('admin.login');
    }
    //登陆验证
    public  function dologin(Request $request)
    {
        //读取用户信息
        $res = DB::table('users')->where('username',$request->input('username'))->first();
        //匹配密码
        if(!$res){
            return back();
        }else{
            if(Hash::check($request->input('password'),$res->password)){
                //写入session
               session(['id'=>$res->id,'name'=>$res->username,'profil'=>$res->profile]);
                return  redirect('/admin');
            }else{
                return back();
            }
        }
    }
    public function delsession(Request $request)
    {
        // session_unset(session('name'));
        // unset(session(['name']);
        $request->session()->forget(['name','id']); 
        return redirect('/admin/login');
    }

    public  function amend(Request $request)
    {
       
         // 读取当前id的用户信息
        $id =  session('id');
        //读取数据库
        $user = DB::table('users')->where('id', $id)->first();
        //解析模板 
        return view('admin.dologin', ['user'=>$user]);
    }

    public function doamend(Request $request)
    {
       
       //读取用户信息
        $res = DB::table('users')->where('id',$request->input('id'))->first();
         //匹配密码
        if(!$res){
            return back();
        }else{
            if(Hash::check($request->input('password'),$res->password)){
               //sdd($request->input('repassword')); 
            DB::table('users')->where('id', $request->input('id'))->update(['password'=>Hash::make($request->input('repassword'))]);
            $request->session()->forget(['name','id']);
            return redirect('/admin/login');
            }else{
                return back();
            }
        }

    }

}
